/* ========================================================================
 * Copyright (c) 2005-2016 The OPC Foundation, Inc. All rights reserved.
 *
 * OPC Foundation MIT License 1.00
 *
 * Permission is hereby granted, free of charge, to any person
 * obtaining a copy of this software and associated documentation
 * files (the "Software"), to deal in the Software without
 * restriction, including without limitation the rights to use,
 * copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following
 * conditions:
 *
 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
 * OTHER DEALINGS IN THE SOFTWARE.
 *
 * The complete license agreement can be found here:
 * http://opcfoundation.org/License/MIT/1.00/
 * ======================================================================*/

namespace Opc.Ua.Types {
    using Opc.Ua.Types.Schema;
    using Opc.Ua.Design.Schema;
    using System.Collections.Generic;
    using System.Xml;

    /// <summary>
    /// Type dictionary extension
    /// </summary>
    public static class TypeDictionaryEx {

        /// <summary>
        /// Convert to model design
        /// </summary>
        /// <param name="types"></param>
        /// <returns></returns>
        public static ModelDesign ToModelDesign(this ITypeDictionary types) {
            var namespaceUri = types.TargetNamespace;
            if (namespaceUri == "http://opcfoundation.org/UA/Core/") {
                namespaceUri = Namespaces.OpcUa;
            }
            // Convert to node declartions
            var nodes = new List<NodeDesign>();
            foreach (var dataType in types.Items) {
                var id = new XmlQualifiedName(dataType.Name, namespaceUri);
                var design = new DataTypeDesign {
                    SymbolicId = id,
                    SymbolicName = id,
                    BaseType = Constants.BaseDataType,
                    NoArraysAllowed = !dataType.AllowArrays,
                    NoClassGeneration = dataType.NotInAddressSpace,
                    NotInAddressSpace = dataType.NotInAddressSpace,
                    IsAbstract = false,
                    Description = dataType.Documentation.ToLocalizedText(),
                    Category = dataType.Category,
                    ReleaseStatus = (Design.Schema.ReleaseStatus)(int)dataType.ReleaseStatus,
                    Purpose = (Design.Schema.DataTypePurpose)(int)dataType.Purpose
                };
                switch (dataType) {
                    case TypeDeclaration simpleType:
                        design.SetDataType(simpleType);
                        nodes.Add(design);
                        break;
                    case ComplexType complexType:
                        design.SetDataType(complexType);
                        nodes.Add(design);
                        break;
                    case ServiceType serviceType:
                        // Not supported
                        break;
                    case EnumeratedType enumeratedType:
                        design.SetDataType(enumeratedType);
                        nodes.Add(design);
                        break;
                }
            }
            return new ModelDesign {
                Items = nodes.ToArray(),
                TargetNamespace = Namespaces.OpcUa,
                TargetVersion = types.TargetVersion,
                TargetPublicationDate = types.TargetPublicationDate,
                TargetPublicationDateSpecified = true,
            };
        }


        /// <summary>
        /// Convert documentation to design localized text
        /// </summary>
        /// <param name="documentation"></param>
        /// <returns></returns>
        public static LocalizedText ToLocalizedText(this Documentation documentation) {
            if (documentation != null &&
                documentation.Text != null &&
                documentation.Text.Length > 0) {
                return new LocalizedText {
                    Value = documentation.Text[0],
                    IsAutogenerated = false
                };
            }
            return null;
        }
    }
}

